package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.event.util.EventUtil;
import com.mycache.annotation.CacheDel;
import com.mycache.annotation.CacheUsing;
import com.qf.dao.HotalDao;
import com.qf.entity.*;
import com.qf.service.HotalService;
import com.qf.service.RoomPriceService;
import com.qf.service.RoomService;
import com.qf.util.EventTypeContact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * (Hotal)表服务实现类
 *
 * @author makejava
 * @since 2020-11-13 09:12:11
 */
@Service("hotalService")
public class HotalServiceImpl extends ServiceImpl<HotalDao, Hotal> implements HotalService {

    @Autowired
    private EventUtil eventUtil;

    @Autowired
    private RoomService roomService;

    @Autowired
    private RoomPriceService roomPriceService;

    @Override
    @CacheDel(key = "'hotallist'")
    public boolean save(Hotal entity) {
        boolean flag = super.save(entity);
        if (flag){
            //酒店信息保存成功，发布酒店新增的事件
            eventUtil.sendMsg(EventTypeContact.HOTAL_INSERT, entity);
        }
        return flag;
    }

    /**
     * 查询所有的酒店信息 - 连带的所有酒店客房以及客房价格都查询出来
     * @return
     */
    @Override
    @CacheUsing(key = "'hotallist'")
    public List<Hotal> list() {
        List<Hotal> hotals = super.list();

        hotals.stream().forEach(hotal -> {
            //通过酒店获得酒店的所有客房信息
            QueryWrapper<Room> queryWrapper = new QueryWrapper<Room>()
                    .eq("hid", hotal.getId());
            List<Room> rooms = roomService.list(queryWrapper);

            //通过客房获取客房的价格信息
            rooms.stream().forEach(room -> {
                //通过客房获取价格
                QueryWrapper<RoomPrice> queryWrapper1 = new QueryWrapper<RoomPrice>()
                        .eq("rid", room.getId());
                List<RoomPrice> prices = roomPriceService.list(queryWrapper1);

                room.setRoomPrices(prices);
            });

            hotal.setRooms(rooms);
        });

        return hotals;
    }

    @Override
    @CacheUsing(key = "'hotal_' + #id")
    public Hotal getById(Serializable id) {

        //查询酒店
        Hotal hotal = super.getById(id);

        //通过酒店获得酒店的所有客房信息
        QueryWrapper<Room> queryWrapper = new QueryWrapper<Room>()
                .eq("hid", hotal.getId());
        List<Room> rooms = roomService.list(queryWrapper);

        //通过客房获取客房的价格信息
        rooms.stream().forEach(room -> {
            //通过客房获取价格
            QueryWrapper<RoomPrice> queryWrapper1 = new QueryWrapper<RoomPrice>()
                    .eq("rid", room.getId());
            List<RoomPrice> prices = roomPriceService.list(queryWrapper1);

            room.setRoomPrices(prices);
        });

        hotal.setRooms(rooms);

        return hotal;
    }

    /**
     * 计算价格明细
     * @param orderParams
     */
    @Override
    public OrderDetailResult orderPrice(OrderParams orderParams) {

        //获得房间以及价格信息
        Room room = roomService.getById(orderParams.getRid());

        //总价的封装对象
        BigDecimal allPrice = BigDecimal.valueOf(0);

        //明细列表
        List<String> priceDetails = new ArrayList<>(); //2020-11-13 2X199

        //时间类型格式转换器
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //循环价格列表 计算总价与明细
        for (RoomPrice roomPrice : room.getRoomPrices()) {
            //判断房间价格的日期是否在用户选择范围内
            if (roomPrice.getDate().getTime() >= orderParams.getBeginTime().getTime()
                    && roomPrice.getDate().getTime() < orderParams.getEndTime().getTime()) {

                //总价格的累加
                allPrice = allPrice.add(BigDecimal.valueOf(roomPrice.getPrice()).multiply(BigDecimal.valueOf(orderParams.getNumber())));

                //计算明细
                String priceDetail = sdf.format(roomPrice.getDate()) + " " + orderParams.getNumber() + "x" + roomPrice.getPrice();
                priceDetails.add(priceDetail);
            }
        }

        //封装返回值
        OrderDetailResult orderDetailResult = new OrderDetailResult()
                .setAllPrice(allPrice.doubleValue())
                .setPriceDetails(priceDetails);
        return orderDetailResult;

    }
}